package com.property.demo.repository;

import com.property.demo.model.Owner;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface OwnerRepository extends JpaRepository<Owner, Long>, JpaSpecificationExecutor<Owner> {
    
    List<Owner> findByNameContaining(String name);
    
    Optional<Owner> findByIdCard(String idCard);
    
    List<Owner> findByPhoneNumber(String phoneNumber);
    
    Boolean existsByIdCard(String idCard);
    
    Boolean existsByPhoneNumber(String phoneNumber);
    
    @Query("SELECT COUNT(DISTINCT h.owner) FROM Home h WHERE h.building.community.id = :communityId AND h.owner IS NOT NULL")
    long countByCommunityId(@Param("communityId") Long communityId);
} 